package com.cskaoyan.controller;

/**
 * @author pengyehua
 * @date 2020/12/5 22:51
 */

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Brand;
import com.cskaoyan.bean.BrandBO;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.service.AdminBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商场管理模块的品牌制造商部分
 */
@RestController
@RequestMapping("admin/brand")
public class AdminBrandController {

    @Autowired
    AdminBrandService adminBrandService;

    /**
     * 查询品牌商信息
     * @return
     * @param brandBO
     */
    @RequestMapping("list")
    public BaseRespVo list(BrandBO brandBO){
//        System.out.println(brandBO);
        ListData<Brand> brandList = adminBrandService.queryBrand(brandBO);

        return BaseRespVo.ok(brandList);
    }

    /**
     * 添加品牌商信息
     * @param brand
     * @return
     */
    @PostMapping("create")
    public BaseRespVo create(@RequestBody Brand brand){
        Brand brandVo = adminBrandService.insertBrand(brand);
        return BaseRespVo.ok(brandVo);
    }

    /**
     * 删除品牌商信息
     * @param brand
     * @return
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Brand brand){
        int count = adminBrandService.queryGoodsCount(brand.getId());
        if(count > 0){
            return BaseRespVo.fail("该品牌商下还有商品，不能被删除");
        }
        adminBrandService.deleteBrand(brand);
        return BaseRespVo.ok();
    }

    /**
     * 修改品牌商信息
     * @param brand
     * @return
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Brand brand){
        Brand brandVo = adminBrandService.updateBrand(brand);
        return BaseRespVo.ok(brandVo);
    }


}
